/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.apache.ibatis.annotations.Update;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.MaintainPlanContentExt;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import org.springblade.modules.repair.service.IMaintainPlanContentExtService;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.IWxFaultCauseService;
import org.springblade.modules.repair.vo.ElevatorSimpleInfoVO;
import org.springblade.modules.repair.vo.PlanAffairDetailVO;
import org.springblade.modules.repair.vo.check.CheckChildPlanListElevatorVo;
import org.springblade.modules.repair.vo.plan.CheckChildPlanListVo;
import org.springblade.modules.repair.vo.plan.DictListVo;
import org.springblade.modules.system.service.IDictService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.repair.entity.MaintainPlanCheck;
import org.springblade.modules.repair.vo.MaintainPlanCheckVO;
import org.springblade.modules.repair.wrapper.MaintainPlanCheckWrapper;
import org.springblade.modules.repair.service.IMaintainPlanCheckService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * 质检工单表 控制器
 *
 * @author ysq
 * @since 2023-11-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("repait/plan/check")
@Api(value = "质检工单表", tags = "质检工单表接口")
public class MaintainPlanCheckController extends BladeController {

	private final IMaintainPlanCheckService maintainPlanCheckService;
	private final IMaintainPlanContentService maintainPlanContentService;
	private final IDictService dictService;
	private final IWxFaultCauseService wxFaultCauseService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情")
	public R<MaintainPlanCheckVO> detail(@ApiParam(value = "工单id", required = true) @RequestParam Long planId) {
		MaintainPlanCheckVO detail = maintainPlanCheckService.detail(planId);
		return R.data(detail);
	}

	/**
	 * 详情
	 * 用于前端缓存功能
	 */
	@GetMapping("/detail/v2")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情")
	public R<MaintainPlanCheck> detailV2(@ApiParam(value = "工单id", required = true) @RequestParam Long planId) {
		MaintainPlanCheck detail = maintainPlanCheckService.getOne(Wrappers.<MaintainPlanCheck>lambdaQuery().eq(MaintainPlanCheck::getPlanId, planId));
		return R.data(detail);
	}


	@PostMapping("/update")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "修改")
	public R<Boolean> finish(@RequestBody MaintainPlanCheck maintainPlanCheck) {
		MaintainPlanCheck planCheck = maintainPlanCheckService.getById(maintainPlanCheck.getId());
		planCheck.setPlanDesc(null);
		maintainPlanCheck.setPlanId(planCheck.getPlanId());
		return R.data(maintainPlanCheckService.finish(maintainPlanCheck));
	}

	@GetMapping("getDictList")
	@ApiOperation(value = "字典")
	public R<DictListVo> getDictList() {
		DictListVo dictListVo = new DictListVo();
		dictListVo.setFaultReason(dictService.getList("fault_reason"));
		dictListVo.setFaultReasonClassified(dictService.getList("fault_reason_classified"));
		dictListVo.setFaultReasonWho(dictService.getList("fault_reason_who"));
		dictListVo.setTreeList(wxFaultCauseService.getTreeList());
		dictListVo.setWxFaultTypeVOS(wxFaultCauseService.getTypeList());
		return R.data(dictListVo);
	}

	@GetMapping("getCheckChildPlanList")
	@ApiOperation(value = "获取子工单列表")
	public R<CheckChildPlanListElevatorVo> getCheckChildPlanList(Long planId) {
		ElevatorSimpleInfoVO elevatorSimpleInfo = maintainPlanContentService.getElevatorSimpleInfo(planId);
		CheckChildPlanListElevatorVo checkChildPlanListElevatorVo = new CheckChildPlanListElevatorVo();
		List<CheckChildPlanListVo> list = maintainPlanCheckService.getCheckChildPlanList(planId);
		checkChildPlanListElevatorVo.setList(list);
		checkChildPlanListElevatorVo.setElevatorAddress(elevatorSimpleInfo.getElevatorAddress());
		checkChildPlanListElevatorVo.setBuildingName(elevatorSimpleInfo.getBuildingName());
		return R.data(checkChildPlanListElevatorVo);
	}
}
